package com.smartnsoft.fractory;

import java.util.ArrayList;
import java.util.List;

import org.fractals.Mandelbrot;
import org.jfract.abstractBeans.AbstractFractalBean;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.smartnsoft.droid4me.app.WrappedSmartListActivity;
import com.smartnsoft.droid4me.framework.DetailsProvider;
import com.smartnsoft.droid4me.framework.DetailsProvider.BusinessViewWrapper;
import com.smartnsoft.droid4me.framework.DetailsProvider.ObjectEvent;

/**
 * The fractal model selector screen.
 * 
 * @author Willy Noel
 * @since 2011.05.25
 */

public final class ModelSelectorActivity
    extends WrappedSmartListActivity<Void>
{

  public class ModelFractarHeaderWrappers
      extends BusinessViewWrapper<Void>
  {

    private int type;

    public ModelFractarHeaderWrappers(int type)
    {
      super(null);
      this.type = type;
    }

    @Override
    protected View createNewView(Activity activity, Void arg0)
    {

      return activity.getLayoutInflater().inflate(R.layout.model_selector_header, null);
    }

    @Override
    protected Object extractNewViewAttributes(Activity arg0, View arg1,
        Void arg2)
    {

      return null;
    }

    @Override
    protected void updateView(Activity arg0, Object arg1, View arg2, Void arg3,
        int arg4)
    {

    }

    @Override
    public int getType(int position, Void businessObjectClass)
    {

      return type;
    }

  }

  public class ModelFractalAttributes
  {

    private final ImageView icon;

    private final TextView name;

    private final TextView expression;

    private RelativeLayout layoutModelSelector;

    public ModelFractalAttributes(View view)
    {
      icon = (ImageView) view.findViewById(R.id.iconModel);
      name = (TextView) view.findViewById(R.id.name);
      expression = (TextView) view.findViewById(R.id.expression);
      layoutModelSelector = (RelativeLayout) view.findViewById(R.id.layoutModelSelector);
    }

    public void update(Context context, AbstractFractalBean fractal)
    {
      setIcon(context, icon, fractal.getType());
      name.setText(fractal.getName());
      expression.setText(fractal.getExpression());
      //TODO : See with Edouard about the list color background.
      layoutModelSelector.setBackgroundResource(R.drawable.model_selector_item_background);

    }

  }

  public class ModelFractalWrapper
      extends BusinessViewWrapper<AbstractFractalBean>
  {

    private int type;

    public ModelFractalWrapper(AbstractFractalBean fractal, int type)
    {
      super(fractal);
      this.type = type;
    }

    @Override
    protected View createNewView(Activity activity, AbstractFractalBean fractal)
    {
      return activity.getLayoutInflater().inflate(R.layout.model_selector_screen, null);
    }

    @Override
    protected Object extractNewViewAttributes(Activity activity, View view,
        AbstractFractalBean fractal)
    {
      return new ModelFractalAttributes(view);
    }

    @Override
    protected void updateView(Activity activity, Object attributes, View view,
        AbstractFractalBean fractal, int position)
    {
      ((ModelFractalAttributes) attributes).update(activity.getApplicationContext(), fractal);

    }

    @Override
    public Intent computeIntent(Activity activity, Object viewAttributes,
        View view, AbstractFractalBean fractal, ObjectEvent objectEvent)
    {
      if (objectEvent == ObjectEvent.Clicked)
      {
        activity.finish();
        return new Intent(activity, EditorActivity.class).putExtra(EditorActivity.FRACTAL, fractal);

      }
      return super.computeIntent(activity, viewAttributes, view, fractal, objectEvent);
    }

    @Override
    public int getType(int position, AbstractFractalBean businessObjectClass)
    {

      return type;
    }

  }

  @Override
  public int getViewTypeCount()
  {
    return 2;
  }

  public List<? extends BusinessViewWrapper<?>> retrieveBusinessObjectsList()
      throws BusinessObjectUnavailableException
  {
    final List<BusinessViewWrapper<?>> listViewWrapper = new ArrayList<DetailsProvider.BusinessViewWrapper<?>>();

    listViewWrapper.add(new ModelFractarHeaderWrappers(0));
 //   List<Mandelbrot> listFracalModel = null;
    
//    try
//    {
//      listFracalModel = FractoryServices.getInstance().getFractalModel(this);
//    }
//    catch (CallException exception)
//    {
//      throw new BusinessObjectUnavailableException(exception);
//    }
//    catch (NotFoundException exception)
//    {
//      throw new BusinessObjectUnavailableException(exception);
//    }
//    catch (SAXException exception)
//    {
//      throw new BusinessObjectUnavailableException(exception);
//    }
    
//    for(AbstractFractalBean fractal : listFracalModel)
//    {
//      listViewWrapper.add(new ModelFractalWrapper(fractal, 1));
//    }

    listViewWrapper.add(new ModelFractalWrapper(new Mandelbrot(getApplicationContext(), Mandelbrot.MANDELBROT_DEFAULT), 1));
    listViewWrapper.add(new ModelFractalWrapper(new Mandelbrot(getApplicationContext(), Mandelbrot.MANDELBROT_CUBIC), 1));
    listViewWrapper.add(new ModelFractalWrapper(new Mandelbrot(getApplicationContext(), Mandelbrot.MANDELBROT_QUATRIC), 1));
    listViewWrapper.add(new ModelFractalWrapper(new Mandelbrot(getApplicationContext(), Mandelbrot.MANDELBROT_QUINTIC), 1));
    listViewWrapper.add(new ModelFractalWrapper(new Mandelbrot(getApplicationContext(), Mandelbrot.MANDELBROT_SEXTIC), 1));

    return listViewWrapper;
  }

  public void setIcon(Context context, ImageView icon, int type)
  {
    switch (type)
    {
    case 0:

      icon.setImageDrawable(context.getResources().getDrawable(R.drawable.mandelbrot_icon_1));

      break;
    case 1:

      icon.setImageDrawable(context.getResources().getDrawable(R.drawable.mandelbrot_icon_2));

      break;
    case 2:

      icon.setImageDrawable(context.getResources().getDrawable(R.drawable.mandelbrot_icon_3));

      break;
    case 3:

      icon.setImageDrawable(context.getResources().getDrawable(R.drawable.mandelbrot_icon_4));

      break;
    case 4:

      icon.setImageDrawable(context.getResources().getDrawable(R.drawable.mandelbrot_icon_5));

      break;
    default:

      icon.setImageDrawable(context.getResources().getDrawable(R.drawable.mandelbrot_icon_1));

      break;
    }
  }

}
